<template>
  <div class="zcyx-box">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>邮箱注册</span>
        </div>
      </template>
      <div>
        <el-form>
          <el-form-item label="邮箱地址">
            <el-input v-model="user.email"
          /></el-form-item>
          <el-form-item label="密码">
            <el-input v-model="user.password"
          /></el-form-item>
          <el-form-item label="验证码">
            <el-input show-password v-model="user.code"></el-input>
            <el-button type="primary" @click="codes">发送验证码</el-button>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button type="primary" @click="zcyx">注册</el-button>
        <el-button type="primary">
          <a href="/user/login">返回登录</a></el-button
        ></template
      >
    </el-card></div
  >
</template>
<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import { Api } from '../../script/Api';

const user = ref({
  email: '',
  password: '',
  code: '',
});

// 定义发送验证码方法
const codes = () => {
  if (!user.value.email) {
    ElMessage.error('请先输入手机号');
    return;
  }

  Api.post('/tools/sendMailCode', { email: user.value.email }, (data) => {
    if (data.success) {
      ElMessage.success(data.message);
    } else {
      ElMessage.error(data.message);
    }
  });
};

const zcyx = () => {
  Api.post('/user/email', user.value, (data) => {
    if (data.success) {
      ElMessage.success(data.message);
    } else {
      ElMessage.error(data.message);
    }
  });
};
</script>

<style scoped>
.zcyx-box {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
